﻿using System.Collections.Generic;
using ML.Entity;
using ML.IRepository;
using Dapper;
using System.Linq;
using MySql.Data.MySqlClient;

namespace ML.Repository
{
    public class ReceivingAddressRepository : DBFactory, IReceivingAddressRepository
    {
        public IEnumerable<ReceivingAddressEntity> GetAddRessByUserId(string id)
        {
            const string sql = @"SELECT * FROM ml_receivingaddress WHERE UserId=@UserId";
            using (_db)
            {
                return _db.Query<ReceivingAddressEntity>(sql, new { @UserId = id });
            }
        }

        public IEnumerable<ReceivingAddressEntity> GetListByUserId(string id)
        {
            const string sql = @"SELECT * FROM ml_receivingaddress WHERE UserId=@UserId AND DelMark=0 ORDER BY CreateTime DESC";
            using (_db)
            {
                return _db.Query<ReceivingAddressEntity>(sql, new { @UserId = id });
            }
        }

        public ReceivingAddressEntity GetDefaulByUserId(string id)
        {
            const string sql = @"SELECT * FROM ml_receivingaddress WHERE UserId=@UserId AND DelMark=0 AND DefaultMark = 1";
            using (_db)
            {
                return _db.Query<ReceivingAddressEntity>(sql, new { @UserId = id }).FirstOrDefault();
            }
        }

        public ReceivingAddressEntity GetById(string id)
        {
            const string sql = @"SELECT * FROM ml_receivingaddress WHERE Id=@Id AND DelMark=0";
            using (_db)
            {
                return _db.Query<ReceivingAddressEntity>(sql, new { @Id = id }).FirstOrDefault();
            }
        }

        public bool AddInfo(ReceivingAddressEntity entity)
        {
            //两个数据库添加的Sql语句
            const string update_sql = @"UPDATE ml_receivingaddress SET DefaultMark=0 WHERE UserId=@UserId";
            const string add_sql = @"INSERT INTO ml_receivingaddress(Id,UserId,ContactName,Contact,Address,DefaultMark,DelMark,CreateTime) VALUES(@Id,@UserId,@ContactName,@Contact,@Address,@DefaultMark,@DelMark,@CreateTime)";

            //构造事务、要么执行成功要么执行失败
            using (var conn = OpenConnection())
            {
                MySqlTransaction trans = conn.BeginTransaction();
                var isUserTrue = conn.Execute(update_sql, new { @UserId = entity.UserId }, trans);
                if (isUserTrue >= 0)
                {
                    var isuserInvitationTrue = conn.Execute(add_sql, entity, trans);
                    if (isuserInvitationTrue < 0)
                    {
                        trans.Rollback();
                        return false;
                    }
                    trans.Commit();
                }
                else
                {
                    trans.Rollback();
                    return false;
                }
                return true;
            }
        }

        public bool Add(ReceivingAddressEntity entity)
        {
            const string add_sql = @"INSERT INTO ml_receivingaddress(Id,UserId,ContactName,Contact,Address,DefaultMark,DelMark,CreateTime) VALUES(@Id,@UserId,@ContactName,@Contact,@Address,@DefaultMark,@DelMark,@CreateTime)";
            using (_db = OpenConnection())
            {
                var istrue = _db.Execute(add_sql, entity);
                if (istrue > 0)
                {
                    return true;
                }
            }
            return false;
        }

        public bool UpdateInfo(ReceivingAddressEntity entity)
        {
            const string update_sql = @"UPDATE ml_receivingaddress SET DefaultMark=0 WHERE UserId=@UserId";
            const string update_address_sql = @"UPDATE ml_receivingaddress SET ContactName=@ContactName,Contact=@Contact,Address=@Address,DefaultMark=@DefaultMark WHERE Id=@Id";

            //构造事务、要么执行成功要么执行失败
            using (var conn = OpenConnection())
            {
                MySqlTransaction trans = conn.BeginTransaction();
                var isUserTrue = conn.Execute(update_sql, new { @UserId = entity.UserId }, trans);
                if (isUserTrue >= 0)
                {
                    var isuserInvitationTrue = conn.Execute(update_address_sql, entity, trans);
                    if (isuserInvitationTrue < 0)
                    {
                        trans.Rollback();
                        return false;
                    }
                    trans.Commit();
                }
                else
                {
                    trans.Rollback();
                    return false;
                }
                return true;
            }
        }

        public bool Update(ReceivingAddressEntity entity)
        {
            const string sql = @"UPDATE ml_receivingaddress SET ContactName=@ContactName,Contact=@Contact,Address=@Address,DefaultMark=@DefaultMark WHERE Id=@Id";
            using (_db)
            {
                if (_db.Execute(sql, entity) > 0)
                {
                    return true;
                }
                return false;
            }
        }

        public bool Default(string id, string userid)
        {
            const string update_default_sql = @"UPDATE ml_receivingaddress SET DefaultMark=0 WHERE UserId=@UserId";
            const string update_sql = @"UPDATE ml_receivingaddress SET DefaultMark=1 WHERE Id=@Id";

            //构造事务、要么执行成功要么执行失败
            using (var conn = OpenConnection())
            {
                MySqlTransaction trans = conn.BeginTransaction();
                var isUserTrue = conn.Execute(update_default_sql, new { @UserId = userid }, trans);
                if (isUserTrue > 0)
                {
                    var isuserInvitationTrue = conn.Execute(update_sql, new { @Id = id }, trans);
                    if (isuserInvitationTrue < 0)
                    {
                        trans.Rollback();
                        return false;
                    }
                    trans.Commit();
                }
                else
                {
                    trans.Rollback();
                    return false;
                }
                return true;
            }
        }

        public bool Delete(ReceivingAddressEntity entity)
        {
            throw new System.NotImplementedException();
        }

        public bool Delete(string id)
        {
            const string sql = @"UPDATE ml_receivingaddress SET DelMark=1 WHERE Id=@Id";
            using (_db)
            {
                if (_db.Execute(sql, new { @Id = id }) > 0)
                {
                    return true;
                }
                return false;
            }
        }
    }
}